package org.dromara.tearoom.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.tearoom.domain.bo.TerAccountsBo;
import org.dromara.tearoom.domain.vo.TerAccountsVo;
import org.dromara.tearoom.domain.vo.WxSignParam;
import org.dromara.tearoom.service.ITerAccountsService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 账户
 * 前端访问路由地址为:/tearoom/accounts
 *
 * @author xch
 * @date 2025-05-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/accounts")
public class TerAccountsController extends BaseController {

    private final ITerAccountsService terAccountsService;

    /**
     * 查询账户列表
     */
    @SaCheckPermission("tearoom:accounts:list")
    @GetMapping("/list")
    public TableDataInfo<TerAccountsVo> list(TerAccountsBo bo, PageQuery pageQuery) {
        return terAccountsService.queryPageList(bo, pageQuery);
    }
    @PostMapping("/payOrder")
    public WxSignParam payOrder(@Validated(AddGroup.class) @RequestBody TerAccountsBo bo) {
        return terAccountsService.payAccount(bo);
    }
    @GetMapping("/queryBalance")
    public R<Integer> queryBalance(
        @RequestParam("start") String start,
        @RequestParam("end") String end){
        return R.ok(terAccountsService.queryBalance(start,end));
    }
    @PostMapping("/unfrozen")
    public R<Void> unfrozen(@Validated(AddGroup.class) @RequestBody TerAccountsBo bo) {
        return toAjax(terAccountsService.unfrozen(bo));
    }

    @GetMapping("/{type}")
    public R<TerAccountsVo> callBackInfo(@NotNull(message = "类型不能为空")
                                             @PathVariable("type") Integer type) {
        if(null==LoginHelper.getAppLoginUser()) {
            return R.ok(terAccountsService.queryById(type.longValue()));
        }else{
            return R.ok(terAccountsService.queryById(type));
        }
    }
    /**
     * 导出账户列表
     */
    @SaCheckPermission("tearoom:accounts:export")
    @Log(title = "账户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TerAccountsBo bo, HttpServletResponse response) {
        List<TerAccountsVo> list = terAccountsService.queryList(bo);
        ExcelUtil.exportExcel(list, "账户", TerAccountsVo.class, response);
    }

    /**
     * 新增账户
     */
    @SaCheckPermission("tearoom:accounts:add")
    @Log(title = "账户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TerAccountsBo bo) {
        return toAjax(terAccountsService.insertByBo(bo));
    }

    /**
     * 修改账户
     */
    @SaCheckPermission("tearoom:accounts:edit")
    @Log(title = "账户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TerAccountsBo bo) {
        return toAjax(terAccountsService.updateByBo(bo));
    }


    /**
     * 删除账户
     *
     * @param ids 主键串
     */
    @SaCheckPermission("tearoom:accounts:remove")
    @Log(title = "账户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable("ids") Long[] ids) {
        return toAjax(terAccountsService.deleteWithValidByIds(List.of(ids), true));
    }



}
